<?php
declare (strict_types = 1);

namespace yifang\console;

use yifang\console\output\Descriptor;

class Output
{
    // 不显示信息(静默)
    const VERBOSITY_QUIET        = 0;
    // 正常信息
    const VERBOSITY_NORMAL       = 1;
    // 详细信息
    const VERBOSITY_VERBOSE      = 2;
    // 非常详细的信息
    const VERBOSITY_VERY_VERBOSE = 3;
    // 调试信息
    const VERBOSITY_DEBUG        = 4;

    const OUTPUT_NORMAL = 0;
    const OUTPUT_RAW    = 1;
    const OUTPUT_PLAIN  = 2;

    // 输出信息级别
    private $verbosity = self::VERBOSITY_NORMAL;

    private $handle = null;

    protected $styles = [
        'info',
        'error',
        'comment',
        'question',
        'highlight',
        'warning',
    ];

    public function __construct($driver = 'console')
    {
        $class = '\\yifang\\console\\output\\driver\\' . ucwords($driver);
        $this->handle = new $class($this);
    }

    /**
     * 输出信息并换行
     */
    public function writeln($messages,$type = 0)
    {
        $this->write($messages, true, $type);
    }

    /**
     * 输出信息
     */
    public function write($messages,$newline = false,$type = 0)
    {
        $this->handle->write($messages, $newline, $type);
    }

    /**
     * 输出描述
     * @param $object
     * @param array $options
     */
    public function describe($object, array $options = [])
    {
        $descriptor = new Descriptor();
        $options    = array_merge([
            'raw_text' => false,
        ], $options);
        $descriptor->describe($this, $object, $options);
    }

    /**
     * 设置输出信息级别
     * @param int $level 输出信息级别
     */
    public function setVerbosity($level)
    {
        $this->verbosity = $level;
    }

    /**
     * 获取输出信息级别
     * @return int
     */
    public function getVerbosity()
    {
        return $this->verbosity;
    }
}